package sto.orz.html.form;

import java.lang.reflect.Field;
import org.apache.commons.lang.StringUtils;

import sto.orz.commons.DataTypeUtils;
import sto.orz.commons.ResourceUtils;
import sto.orz.html.*;

public class ElementAttribute {
	ElementType elementType;

	ExtendMode extendMode;

	String label = "";

	String mapping;
	String name;
	Class dataType;
	String dataUri;

	public ElementAttribute(Field field) throws AnnotationNotFoundException,
			AnnotationNotValidException {
		Element fe;
		try {
			fe = field.getAnnotation(Element.class);
		} catch (NullPointerException e) {
			throw new AnnotationNotFoundException(e);
		}
		if (!DataTypeUtils.isBasicDataType(field.getType())) {
			try {
				String mapping = fe.mapping();
				if (StringUtils.isBlank(mapping))
					throw new AnnotationNotValidException(
							"如果是关联对象(不是简单数据类型),则必须设置关联字段名");
			} catch (NullPointerException e) {
				throw new AnnotationNotValidException(
						"如果是关联对象(不是简单数据类型),则必须设置关联字段名");
			}
		}

		setName(field.getName());

		setLabel(fe.label());
		setMapping(fe.mapping());

		setElementType(fe.elementType());
		setExtendMode(fe.extendMode());
		setDataType(field.getType());

		if (getElementType() == ElementType.SELECTBOX) {
			try {
				// TODO refact
				Select sb = field.getAnnotation(Select.class);
				Object[] raw = new Object[] { "select ", getMapping(), ",",
						sb.display(), " from ", getDataType().getName(),
						" as ", getName() };

				String hql = StringUtils.join(raw);
				setDataUri(hql);
			} catch (NullPointerException e) {
				throw new AnnotationNotFoundException(e);
			}
		}
	}

	public ElementType getElementType() {
		return elementType;
	}

	public ExtendMode getExtendMode() {
		return extendMode;
	}

	public String getLabel() {
		return label;
	}

	public String getMapping() {
		return mapping;
	}

	public String getName() {
		return name;
	}

	public void setElementType(ElementType elementType) {
		this.elementType = elementType;
	}

	public void setExtendMode(ExtendMode extendMode) {
		this.extendMode = extendMode;
	}

	public void setLabel(String label) {
		this.label = label;
	}

	public void setMapping(String mapping) {
		this.mapping = mapping;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCaption() {
		String caption = label;
		if (StringUtils.isBlank(caption))
			caption = ResourceUtils.getStringOrNull(name);

		if (StringUtils.isBlank(caption))
			caption = name;
		return caption;
	}

	public String getId() {
		if (StringUtils.isNotBlank(mapping))
			return mapping;
		return name;
	}

	public boolean isObject() {

		return true;
	}

	public Class getDataType() {
		return dataType;
	}

	public void setDataType(Class dataType) {
		this.dataType = dataType;
	}

	public String getDataUri() {
		return dataUri;
	}

	public void setDataUri(String dataUri) {
		this.dataUri = dataUri;
	}

}
